package club.itstudio.IContainer.service;

import club.itstudio.IContainer.entity.Course;
import club.itstudio.IContainer.entity.Image;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

@Service
public class CourseService {
    private JdbcTemplate jdbcTemplate;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final CourseMapper courseMapper = new CourseMapper();

    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public boolean insert(Course course) {
        String sql = "insert into course(course_name, description, max_num, teacher_id, add_type, created_time) " +
                "values(?, ?, ?, ?, ?, ?)";
        try {
            int count = jdbcTemplate.update(sql, course.courseName, course.description, course.maxNum,
                    course.teacherID, course.addType, course.createdTime);
            return count == 1;
        }catch (DataAccessException e) {
            logger.info("insert new course error", e);
            return false;
        }
    }

    public Course getCourseByID(int id) {
        String sql = "select * from course where id=?";
        Course course = null;
        try {
            course = jdbcTemplate.queryForObject(sql, courseMapper, id);
        }catch (DataAccessException e) {
            logger.info("error get course by id:" + id);
        }
        return course;
    }

    public boolean delete(int id) {
        String sql = "delete from course where id=?";
        try {
            return jdbcTemplate.update(sql, id) == 1;
        }catch (DataAccessException e) {
            logger.info("error delete course by id:" + id, e);
            return false;
        }
    }

    public List<Course> getCourseList(int currentPage, int pageSize) {
        String sql = "select * from course order by created_time DESC limit ?,?";
        return jdbcTemplate.query(sql, courseMapper, (currentPage - 1) * pageSize, pageSize);
    }

    public List<Course> getCourseListByTeacherID(int currentPage, int pageSize, int teacherID) {
        String sql = "select * from course where teacher_id=? order by created_time DESC limit ?,?";
        return jdbcTemplate.query(sql, courseMapper, teacherID, (currentPage - 1) * pageSize, pageSize);
    }

    public int getCourseNumByTeacherID(int teacherID) {
        String sql = "select count(*) from course where teacher_id=?";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, teacherID);
        return Objects.requireNonNullElse(count, 0);
    }

    public int getCourseNum() {
        String sql = "select count(*) from course";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        return Objects.requireNonNullElse(count, 0);
    }
}

class CourseMapper implements RowMapper<Course> {
    @Override
    public Course mapRow(ResultSet resultSet, int i) throws SQLException {
        Course course = new Course();
        course.id = resultSet.getInt("id");
        course.courseName = resultSet.getString("course_name");
        course.description = resultSet.getString("description");
        course.maxNum = resultSet.getInt("max_num");
        course.teacherID = resultSet.getInt("teacher_id");
        course.addType = resultSet.getInt("add_type");
        course.createdTime = resultSet.getLong("created_time");
        return course;
    }
}
